class DecideController < ApplicationController

  def index
    

    qid = 1
    if params[:id] != nil
      qid = params[:id]
    else
      qid = rand( Question.count ) + 1 #have to add one because ActiveRecord starts with id = 1
    end
    @question = Question.find(qid)    

    aid = rand( @question.answers.length )
    if params[:sid] != nil
      a = Answer.find(params[:sid])
      if a.question.id == @question.id
        @answer = a
      else
        @answer = @question.answers[aid]
      end
    else
      @answer = @question.answers[aid]
    end

   # used for the tag lists

   @questions = Question.find(:all)
  # @answers = @question.answers.sort { |x,y| y.votes <=> x.votes}         
  end

  def voteup
    aid = params[:id]
    @answer = Answer.find(aid)
    @answer.helpful = @answer.helpful + 1
    @answer.save
    # use ajax, so no redirect
    # redirect_to("/decide/index/#{@answer.question.id}/#{@answer.id}")
  end

  def votedown
    aid = params[:id]
    @answer = Answer.find(aid)
    @answer.unhelpful = @answer.unhelpful + 1
    @answer.save
    # use ajax, so no redirect
    # redirect_to("/decide/index/#{@answer.question.id}/#{@answer.id}")
  end

  def answer
    @answer = Answer.new(params[:uanswer])
    @answer.helpful = 0
    @answer.unhelpful = 0
    @answer.save
    redirect_to("/decide/index/#{@answer.question.id}/#{@answer.id}")    
  end

  def all
   @question = Question.find(params[:id])
   @answers = @question.answers.sort { |x,y| y.votes <=> x.votes }
  end

# list all answers
# list of tags

end
